VERSION = 1.0.4

RAW_GO_VERSION := $(shell go version)
GO_VERSION     := $(subst go version go,,${RAW_GO_VERSION})

PACKAGE_NAME := gitee-lfs-multipart-transfer
BUILD_TIME   := $(shell date --rfc-3339=seconds)
BUILD_COMMIT := $(shell git rev-parse HEAD)
ROOT_DIR     := $(shell pwd)
SOURCE_DIR   := $(abspath $(dir $(lastword ${MAKEFILE_LIST})))


COMMON_PACKAGE := ${PACKAGE_NAME}/internal/common
GO_LDFLAGS 	:= -X '${COMMON_PACKAGE}.Version=$(VERSION)' -X '${COMMON_PACKAGE}.BuildTime=$(BUILD_TIME)' -X '${COMMON_PACKAGE}.BuildCommit=$(BUILD_COMMIT)' -X '${COMMON_PACKAGE}.BuildGoVersion=$(GO_VERSION)'
EXECUTABLES := $(notdir $(shell find ${SOURCE_DIR}/cmd -mindepth 1 -maxdepth 1 -type d -print))

GOOS_LIST   := linux windows darwin
GOARCH_LIST := amd64 arm64

export CGO_ENABLED ?= 0
export GOPROXY     ?= https://goproxy.cn

.PHONY: all
all: build

.PHONY: build
build: ${EXECUTABLES}

.PHONY: ${EXECUTABLES}
${EXECUTABLES}:
	go build -o $@ -ldflags "${GO_LDFLAGS}" $(addprefix ${PACKAGE_NAME}/cmd/, $@)

.PROXY: build-all-arch
build-all-arch:
	@for exe in ${EXECUTABLES}; do \
		for goos in ${GOOS_LIST}; do \
			for goarch in ${GOARCH_LIST}; do \
				out_name=$${exe}-$${goos}-$${goarch}; \
				[ "$$goos" = "windows" ] && out_name="$$out_name.exe"; \
				echo "==> Building $$out_name"; \
				GOOS=$$goos GOARCH=$$goarch go build -o $$out_name -ldflags "${GO_LDFLAGS}" $(addprefix ${PACKAGE_NAME}/cmd/, $$exe); \
				echo "==> Generating checksums for $$out_name"; \
				{ \
					echo "MD5= $$(md5sum $$out_name | awk '{print $$1}')"; \
					echo "SHA1= $$(sha1sum $$out_name | awk '{print $$1}')"; \
					echo "SHA256= $$(sha256sum $$out_name | awk '{print $$1}')"; \
				} > $$out_name.dgst; \
			done; \
		done; \
	done
	